<?php


$all = true; //false;


$db=pg_connect('dbname=od user=od password=od');


$array = array();


$heights = array(
		'Низкого роста (до 175)'=>array(1, 174),
		'Среднего роста (175-190)'=>array(175, 189),
		'Высокого роста (от 190)'=>array(190, 300),
);

$ages = array(
		'Младше 20'=>array(0, 20),
		'Младше 30'=>array(0, 30),
		'Младше 40'=>array(0, 40),
		'Младше 50'=>array(0, 50),
		'Младше 60'=>array(0, 60),
		'Старше 20'=>array(21, 100),
		'Старше 30'=>array(31, 100),
		'Старше 40'=>array(41, 100),
		'Старше 50'=>array(51, 100),
		'Старше 60'=>array(61, 100),
);
$types = array(
	1 => 'Игрок',
	2 => 'Тренер',
	3 => 'Из администрации клуба',
	4 => 'Судья',
	5 => 'Руководитель клуба',
);

$types2 = array(
	1 => 'Вратарь',
	2 => 'Защитник',
	3 => 'Полузащитник',
	4 => 'Нападающий',
	5 => 'Тренер',
	6 => 'Из администрации клуба',
	7 => 'Руководитель клуба',
);


// страны

$result = pg_query($db, "select country
	from person group by country");
$countries = array();
while ($country = pg_fetch_assoc($result)) {
	$c = preg_split('/[,\/]/', $country['country']);
	foreach ($c as $cc) {
		$countries[$cc] = 'Гражданство - '.trim($cc);
	}
}

// клубы

$result = pg_query($db, "select person_id, club.id, club.title, type, position, number, 
	ligue.id as ligue_id, ligue.title as ligue_title
	from persons_of_club poc, clubs_of_season cos, club, season, ligue 
	where poc.club_of_season_id = cos.id and club.id = cos.club_id 
	and cos.season_id = season.id and season.ligue_id = ligue.id
	and club.public = true and ligue.public=true and season.public=true");
$clubs = array();
$poc = array();
$positions = array();
$posCount = array();
$numbers = array();
$ligues = array();
$ligues2 = array(); // корневые лиги (схлопываем второй дивизион)

while ($row = pg_fetch_assoc($result)) {
        $poc[$row['person_id']]['club'][$row['id']] = $row['id'];
	if ($row['type']) 
        	$poc[$row['person_id']]['type'][$row['type']] = $row['type'];
        if (trim($row['position'])) 
		$poc[$row['person_id']]['position'][$row['position']] = trim($row['position']);
	if ($row['number']) {
        	$poc[$row['person_id']]['number'][$row['number']] = $row['number'];
		$numbers[$row['number']] = 'Игровой номер ' . $row['number'];
	}

	$clubs[$row['id']] = 'Был в составе клуба "' . trim($row['title']).'"';

	if (trim($row['position'])) {
		$positions[$row['position']] = trim($row['position']);
		@$posCount[$row['position']]++;
	}
	$poc[$row['person_id']]['ligue'][$row['ligue_id']] = $row['ligue_id'];
	$ligues[$row['ligue_id']] = 'Принимал участие в играх лиги "'. trim($row['ligue_title']).'"';
	if (preg_match('/^Второй дивизион/ui', trim($row['ligue_title']))) {
		$ligues2[$row['ligue_id']] = 'Принимал участие в играх лиги "Второй дивизион"';
	} else {
		$ligues2[$row['ligue_id']] = 'Принимал участие в играх лиги "'. trim($row['ligue_title']).'"';
	}
	
}


// достаём всех игроков

$result = pg_query($db, "select 
	id, lastname, firstname, middlename, height, birthday, type, country
	from person 
	where public = true 
	limit 100000");

while ($person = pg_fetch_assoc($result)) {
    $array[$person['id']]['result'] = trim($person['lastname'].' '. $person['firstname'].' '. $person['middlename']);
    	// рост
	if ($person['height'])
	foreach ($heights as $key=>$value) {
		if ($person['height'] >= $value[0] && $person['height'] <= $value[1]) {
			$array[$person['id']]['questions'][$key] = 1;
		} else {
			$array[$person['id']]['questions'][$key] = -1;
		}
	}   

	// возраст
	if ($person['birthday'])
	foreach ($ages as $key=>$value) {
		$age = round((time() - strtotime($person['birthday'])) / (365.25*24*3600));
		if ($age >= $value[0] && $age <= $value[1]) {
			$array[$person['id']]['questions'][$key] = 1;
		} else {
			$array[$person['id']]['questions'][$key] = -1;
		}
	}
	// тип	
	if ($person['type'])
	foreach ($types as $key=>$value) {
		if ($person['type'] == $key) {
			$array[$person['id']]['questions'][$value] = 1;
		} else {
		 	$array[$person['id']]['questions'][$value] = -1;
		}
	}    
	// гражданство
	if ($all && $person['country'])
	foreach ($countries as $key=>$value) {
		$c = preg_split('/[,\/]/', $person['country']);
		if (in_array($key, $c)) {
			$array[$person['id']]['questions'][$value] = 1;
		} else {
		 	$array[$person['id']]['questions'][$value] = -1;
		}
	}    

	// тип2	
	if (@$poc[$person['id']]['type'])
	foreach ($types2 as $key=>$value) {
		if (in_array($key, $poc[$person['id']]['type'])) {
			$array[$person['id']]['questions'][$value] = 1;
		} else {
		 	$array[$person['id']]['questions'][$value] = -1;
		}
	}    
		
	// позиция в клубе	
	if (@$poc[$person['id']]['position'])
	foreach ($positions as $key=>$value) {
		if (in_array($key, $poc[$person['id']]['position'])) {
			$array[$person['id']]['questions'][$value] = 1;
		} else {
			if ($posCount[$key] > 10)
		 		$array[$person['id']]['questions'][$value] = -1;
		}
	}    
	
	// клуб
	if ($all && @$poc[$person['id']]['club'])
	foreach ($clubs as $key=>$value) {
		if (in_array($key, $poc[$person['id']]['club'])) {
			$array[$person['id']]['questions'][$value] = 1;
		} else {
	 		$array[$person['id']]['questions'][$value] = -1;
		}
	}

	// номер в клубе
	if (@$poc[$person['id']]['number'])
	foreach ($numbers as $key=>$value) {
		if (in_array($key, $poc[$person['id']]['number'])) {
			$array[$person['id']]['questions'][$value] = 1;
		} else {
	 		$array[$person['id']]['questions'][$value] = -1;
		}
	}
	
	// турнир
	if (@$poc[$person['id']]['ligue']) {
		foreach ($ligues as $key=>$value) {
			if (in_array($key, $poc[$person['id']]['ligue'])) {
				$array[$person['id']]['questions'][$value] = 1;
			} else {
		 		$array[$person['id']]['questions'][$value] = -1;
			}
		}
		foreach ($ligues2 as $key=>$value) {
			if (in_array($key, $poc[$person['id']]['ligue'])) {
				$array[$person['id']]['questions'][$value] = 1;
			} else {
				$array[$person['id']]['questions'][$value] = -1;
			}
		}
	}

}

echo gzcompress(json_encode($array), 9);

 